<?php

namespace app\admin\controller;

use support\Request;
use app\common\Attribute;
use app\model\Menu;
use app\common\View;

#[Attribute(title: "菜单规则")]
class MenuController
{
    #[Attribute(isAuth: true, isPage: true, title: "查询")]
    public function query(Request $request)
    {
        if ($request->method() == "POST") {
            $all = $request->all();
            $data = Menu::where(function ($data) use ($all) {
                if (isset($all["pid"])) {
                    $data->where("pid", $all["pid"]);
                } else {
                    $data->where("pid", 0)->page($all["page"], $all["limit"]);
                }
            })->order(["weigh" => 'desc'])->select()->each(function ($v, $k) {
                $find = Menu::where("pid", $v["id"])->find();
                if ($find) {
                    $v["isParent"] = true;
                } else {
                    $v["isParent"] = false;
                }
            });
            return json([
                "code" => 0,
                "msg" => "获取成功",
                "count" => count($data),
                "data" => $data->toArray(),
            ]);
        }
        return View::fetch();
    }

    #[Attribute(isAuth: true, isPage: true, title: "添加")]
    public function add(Request $request)
    {
        $parent_data = Menu::where('is_menu', 0)->where('pid', 0)->field('id,name')->select();
        View::assign(name: ["parent_data" => $parent_data]); //顶级选项
        $appAttribute = appAttribute(app: "admin"); //获取app注解
        View::assign("appAttribute", json_encode($appAttribute, 320));
        if ($request->method() == "POST") {
            $all = $request->all();
            try {
                validate("Menu", "add", $all);
                $res = Menu::create($all);
                if ($res) {
                    return success("操作成功");
                } else {
                    return error("操作失败");
                }
            } catch (\think\exception\ValidateException $e) {
                return error(msg: "验证失败", data: $e->getError());
            }
        }
        return View::fetch();
    }

    #[Attribute(isAuth: true, isPage: false, title: "编辑")]
    public function edit(Request $request)
    {
        $parent_data = Menu::where('is_menu', 0)->where('pid', 0)->field('id,name')->select();
        View::assign(name: ["parent_data" => $parent_data]); //顶级选项
        $appAttribute = appAttribute(app: "admin"); //获取app注解
        View::assign("appAttribute", json_encode($appAttribute, 320));
        if ($request->method() == "GET") {
            $id = $request->get('id');
            $find = Menu::find($id);
            if ($find) {
                View::assign("find", $find);
            } else {
                return error("参数错误");
            }
        }
        if ($request->method() == "POST") {
            $all = $request->all();
            try {
                validate("Menu", "edit", $all);
                $res = Menu::update($all);
                if ($res) {
                    return success("操作成功");
                } else {
                    return error("操作失败");
                }
            } catch (\think\exception\ValidateException $e) {
                return error(msg: "验证失败", data: $e->getError());
            }
        }
        return View::fetch();
    }

    #[Attribute(isAuth: true, isPage: false, title: "删除")]
    public function del(Request $request)
    {
        if ($request->method() == "DELETE") {
            $all = $request->all();

            $id  = Menu::where('pid', $all["id"])->value('id');
            if ($id) {
                return error(msg: "请先删除下级");
            }
            $res = Menu::destroy($all["id"]);
            if ($res) {
                return success("操作成功");
            } else {
                return error("操作失败");
            }
        }
    }
}
